package com.chenye.jpa.dao;

import com.chenye.jpa.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Auther:chenye
 * @Data:2018/4/19
 * @Description:
 */

public interface UserDao extends JpaRepository<User,Integer> {
    List<User> findByAgeEqualsAndNameEquals(Integer age,String name);

    @Query(value = "select u from User u where u.name is not null and u.age is not null")
    List<User> testQuery();

    @Query(value = "select u from User u where u.name = :name")
    List<User> testQuery2(@Param("name")String name);


    @Transactional
    @Modifying(clearAutomatically = true)
    @Query(value = "update user set name=?1,age=?2 where id=?3",nativeQuery = true)
    void update(@Param("name")String name,@Param("age")Integer age,@Param("id")Integer id);

    @Transactional
    @Modifying(clearAutomatically = true)
    @Query(value = "update user set name=:name,age=:age where id=:id",nativeQuery = true)
    void update1(@Param("name")String name,@Param("age")Integer age,@Param("id")Integer id);
}
